home *** CD-ROM | disk | FTP | other *** search
-
-
- Polymer('core-iconset', {
-
- /**
- * The URL of the iconset image.
- *
- * @attribute src
- * @type string
- * @default ''
- */
- src: '',
-
- /**
- * The width of the iconset image. This must only be specified if the
- * icons are arranged into separate rows inside the image.
- *
- * @attribute width
- * @type number
- * @default 0
- */
- width: 0,
-
- /**
- * A space separated list of names corresponding to icons in the iconset
- * image file. This list must be ordered the same as the icon images
- * in the image file.
- *
- * @attribute icons
- * @type string
- * @default ''
- */
- icons: '',
-
- /**
- * The size of an individual icon. Note that icons must be square.
- *
- * @attribute iconSize
- * @type number
- * @default 24
- */
- iconSize: 24,
-
- /**
- * The horizontal offset of the icon images in the inconset src image.
- * This is typically used if the image resource contains additional images
- * beside those intended for the iconset.
- *
- * @attribute offsetX
- * @type number
- * @default 0
- */
- offsetX: 0,
- /**
- * The vertical offset of the icon images in the inconset src image.
- * This is typically used if the image resource contains additional images
- * beside those intended for the iconset.
- *
- * @attribute offsetY
- * @type number
- * @default 0
- */
- offsetY: 0,
- type: 'iconset',
-
- created: function() {
- this.iconMap = {};
- this.iconNames = [];
- this.themes = {};
- },
-
- ready: function() {
- // TODO(sorvell): ensure iconset's src is always relative to the main
- // document
- if (this.src && (this.ownerDocument !== document)) {
- this.src = this.resolvePath(this.src, this.ownerDocument.baseURI);
- }
- this.super();
- this.updateThemes();
- },
-
- iconsChanged: function() {
- var ox = this.offsetX;
- var oy = this.offsetY;
- this.icons && this.icons.split(/\s+/g).forEach(function(name, i) {
- this.iconNames.push(name);
- this.iconMap[name] = {
- offsetX: ox,
- offsetY: oy
- }
- if (ox + this.iconSize < this.width) {
- ox += this.iconSize;
- } else {
- ox = this.offsetX;
- oy += this.iconSize;
- }
- }, this);
- },
-
- updateThemes: function() {
- var ts = this.querySelectorAll('property[theme]');
- ts && ts.array().forEach(function(t) {
- this.themes[t.getAttribute('theme')] = {
- offsetX: parseInt(t.getAttribute('offsetX')) || 0,
- offsetY: parseInt(t.getAttribute('offsetY')) || 0
- };
- }, this);
- },
-
- // TODO(ffu): support retrived by index e.g. getOffset(10);
- /**
- * Returns an object containing `offsetX` and `offsetY` properties which
- * specify the pixel locaion in the iconset's src file for the given
- * `icon` and `theme`. It's uncommon to call this method. It is useful,
- * for example, to manually position a css backgroundImage to the proper
- * offset. It's more common to use the `applyIcon` method.
- *
- * @method getOffset
- * @param {String|Number} icon The name of the icon or the index of the
- * icon within in the icon image.
- * @param {String} theme The name of the theme.
- * @returns {Object} An object specifying the offset of the given icon
- * within the icon resource file; `offsetX` is the horizontal offset and
- * `offsetY` is the vertical offset. Both values are in pixel units.
- */
- getOffset: function(icon, theme) {
- var i = this.iconMap[icon];
- if (!i) {
- var n = this.iconNames[Number(icon)];
- i = this.iconMap[n];
- }
- var t = this.themes[theme];
- if (i && t) {
- return {
- offsetX: i.offsetX + t.offsetX,
- offsetY: i.offsetY + t.offsetY
- }
- }
- return i;
- },
-
- /**
- * Applies an icon to the given element as a css background image. This
- * method does not size the element, and it's often necessary to set
- * the element's height and width so that the background image is visible.
- *
- * @method applyIcon
- * @param {Element} element The element to which the background is
- * applied.
- * @param {String|Number} icon The name or index of the icon to apply.
- * @param {Number} scale (optional, defaults to 1) A scaling factor
- * with which the icon can be magnified.
- * @return {Element} The icon element.
- */
- applyIcon: function(element, icon, scale) {
- var offset = this.getOffset(icon);
- scale = scale || 1;
- if (element && offset) {
- var icon = element._icon || document.createElement('div');
- var style = icon.style;
- style.backgroundImage = 'url(' + this.src + ')';
- style.backgroundPosition = (-offset.offsetX * scale + 'px') +
- ' ' + (-offset.offsetY * scale + 'px');
- style.backgroundSize = scale === 1 ? 'auto' :
- this.width * scale + 'px';
- if (icon.parentNode !== element) {
- element.appendChild(icon);
- }
- return icon;
- }
- }
-
- });
-
-